package com.test;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collection;
import java.util.Collections;
import java.util.Properties;
import java.util.concurrent.Callable;

import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.DefaultPropertiesPersister;
import org.springframework.util.PropertiesPersister;

public class PropertiesInnerUtil {

	private static final String DEFAULT_ENCODING = "UTF-8";

	/**
	 * 载入多个properties文件, 相同的属性在最后载入的文件中的值将会覆盖之前的载入. 文件路径使用Spring Resource格式,
	 * 文件编码使用UTF-8.
	 *
	 * @see org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
	 */
	private static Properties loadProperties(String propertyFile) throws IOException {
		return FutureTaskInnerUtil.get(propertyFile, new PropertyFileCall(propertyFile));
	}

	private static class PropertyFileCall implements Callable<Properties> {
		private String propertyFile;
		public PropertyFileCall(String propertyFile){
			this.propertyFile = propertyFile;
		}

		@Override
		public Properties call() throws Exception {
			PropertiesPersister propertiesPersister = new DefaultPropertiesPersister();
			ResourceLoader resourceLoader = new DefaultResourceLoader();
			Properties props = new Properties();
			InputStream is = null;
			try {
				Resource resource = resourceLoader.getResource(propertyFile);
				is = resource.getInputStream();
				propertiesPersister.load(props, new InputStreamReader(is, DEFAULT_ENCODING));
			} catch (IOException ex) {
				ex.printStackTrace();
			} finally {
				if (is != null) {
					is.close();
				}
			}
			return props;
		}

	}

	@SuppressWarnings("unchecked")
	public static Collection<Object> getKeys(String propertyFile) {
		Properties properties;
		try {
			properties = PropertiesInnerUtil.loadProperties(propertyFile);
			return properties.keySet();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return Collections.EMPTY_SET;
	}

	/**
	 * Description:根据key值,从配置文件中读取相关配置信息<br />
	 *
	 * @param propertyFile
	 * @return key
	 */
	public static String getString(String propertyFile, String key) {
		return getString(propertyFile, key, null);
	}

	public static String getString(String propertyFile, String key, String defaultValue) {
		Properties properties;
		String propertyValue = null;
		try {
			properties = PropertiesInnerUtil.loadProperties(propertyFile);
			propertyValue = properties.getProperty(key);
		} catch (IOException e) {
			e.printStackTrace();
		}
		if(propertyValue == null){
			return defaultValue;
		}
		return propertyValue.trim();
	}

	public static Boolean getBoolean(String propertyFile, String key) {
		return getBoolean(propertyFile, key, null);
	}

	public static Boolean getBoolean(String propertyFile, String key, Boolean defaultValue) {
		String val = getString(propertyFile, key, null);
		if(val == null){
			return defaultValue;
		}
        return Boolean.valueOf(val);
    }

	public static Integer getInteger(String propertyFile, String key) {
		return getInteger(propertyFile, key, null);
	}

	public static Integer getInteger(String propertyFile, String key, Integer defaultValue) {
		String val = getString(propertyFile, key, null);
		if(val == null){
			return defaultValue;
		}
		try {
            return Integer.parseInt(val);
        } catch (NumberFormatException nfe) {
            throw new NumberFormatException(" '" + val + "'");
        }
	}

	public static Long getLong(String propertyFile, String key) {
		return getLong(propertyFile, key, null);
	}

	public static Long getLong(String propertyFile, String key, Long defaultValue) {
		String val = getString(propertyFile, key, null);
		if(val == null){
			return defaultValue;
		}
		try {
            return Long.parseLong(val);
        } catch (NumberFormatException nfe) {
            throw new NumberFormatException(" '" + val + "'");
        }
	}

}
